package com.cuiweiyou.gimmick;

import java.util.ArrayList;
import java.util.List;

public class UtilListOrString {

    public static String listToString(List<String> list) {
        final String SEPARATOR = ",";
        StringBuilder sb = new StringBuilder();
        String convertedListStr = "";
        if (null != list && list.size() > 0) {
            for (String item : list) {
                sb.append(item);
                sb.append(SEPARATOR);
            }
            convertedListStr = sb.toString();
            convertedListStr = convertedListStr.substring(0, convertedListStr.length() - SEPARATOR.length());
            return convertedListStr;
        } else {
            return "";
        }
    }

    public static List<String> stringToList(String string) {
        List<String> result = new ArrayList<>();

        if (null == string) {
            return result;
        }

        String trim = string.trim();
        if (trim.length() < 1) {
            return result;
        }

        String[] split = trim.split(Constant.INDEX_SPLITE_FLAG);
        if (split.length < 1) {
            return result;
        }

        for (int i = 0; i < split.length; i++) {
            result.add(split[i]);
        }

        return result;
    }
}
